from django.db import models
from django.utils import timezone


class ParentBoard(models.Model):
    """父板块模型"""
    p_board_id = models.AutoField(primary_key=True, verbose_name='Parent Board ID')
    board_name = models.CharField(max_length=100, verbose_name='Board Name')
    board_desc = models.CharField(max_length=500, blank=True, null=True, verbose_name='Board Description')
    require_permission = models.BooleanField(default=False, verbose_name='Require Posting Permission')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='Create Time')
    update_time = models.DateTimeField(auto_now=True, verbose_name='Update Time')
    
    class Meta:
        db_table = 'parent_boards'
        verbose_name = 'Parent Board'
        verbose_name_plural = 'Parent Boards'
        
    def __str__(self):
        return self.board_name


class Board(models.Model):
    """子板块模型"""
    board_id = models.AutoField(primary_key=True, verbose_name='Board ID')
    p_board = models.ForeignKey(
        ParentBoard, 
        on_delete=models.CASCADE, 
        related_name='children',
        verbose_name='Parent Board'
    )
    board_name = models.CharField(max_length=100, verbose_name='Board Name')
    board_desc = models.CharField(max_length=500, blank=True, null=True, verbose_name='Board Description')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='Create Time')
    update_time = models.DateTimeField(auto_now=True, verbose_name='Update Time')
    
    class Meta:
        db_table = 'boards'
        verbose_name = 'Board'
        verbose_name_plural = 'Boards'
        indexes = [
            models.Index(fields=['p_board']),
        ]
        
    def __str__(self):
        return f"{self.p_board.board_name} - {self.board_name}"


class BoardPostPermission(models.Model):
    """板块发帖权限模型"""
    from apps.users.models import User
    
    id = models.AutoField(primary_key=True, verbose_name='Permission ID')
    p_board = models.ForeignKey(
        ParentBoard,
        on_delete=models.CASCADE,
        verbose_name='Parent Board'
    )
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        verbose_name='User'
    )
    is_active = models.BooleanField(default=True, verbose_name='Is Active')
    granted_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='granted_permissions',
        verbose_name='Granted By'
    )
    create_time = models.DateTimeField(default=timezone.now, verbose_name='Create Time')
    update_time = models.DateTimeField(auto_now=True, verbose_name='Update Time')
    
    class Meta:
        db_table = 'board_post_permissions'
        verbose_name = 'Board Post Permission'
        verbose_name_plural = 'Board Post Permissions'
        unique_together = ['p_board', 'user']
        indexes = [
            models.Index(fields=['p_board']),
            models.Index(fields=['user']),
            models.Index(fields=['is_active']),
        ]
        
    def __str__(self):
        return f"{self.user.nickname} - {self.p_board.board_name} Post Permission"